import 'dart:async';

import 'package:flutter/material.dart';

enum ChatRecordMsgDirection { Left, Right }

class ChatRecordMsg extends StatefulWidget {
  const ChatRecordMsg({
    required this.url,
    required this.isPlay,
    required this.direction,
    Key? key,
  }) : super(key: key);

  final String url;
  final bool isPlay;
  final ChatRecordMsgDirection direction;

  @override
  _ChatRecordMsgState createState() => _ChatRecordMsgState();
}

class _ChatRecordMsgState extends State<ChatRecordMsg> {
  static final images = [
    "images/record/left_voice_1.png",
    "images/record/left_voice_2.png",
    "images/record/left_voice_3.png",
  ];
  int index = images.length - 1;
  Timer? timer;

  @override
  void initState() {
    super.initState();
    timer = Timer.periodic(Duration(milliseconds: 500), (timer) {
      if (!widget.isPlay) {
        index = images.length - 1;
        setState(() {});
        return;
      }
      index++;
      if (index >= images.length) index = 0;
      setState(() {});
    });
  }

  @override
  void dispose() {
    super.dispose();
    timer?.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return RotatedBox(
      quarterTurns: widget.direction == ChatRecordMsgDirection.Left ? 0 : 2,
      child: Image.asset(
        images[index],
        height: 30,
      ),
    );
  }
}
